package com.zhuiYun.project.common.response;

import com.alibaba.fastjson.JSONObject;
import com.zhuiYun.project.util.ObjectUtils;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName ReponseUtil
 * @Description response统一响应结果
 * @Author zcy
 * @Date 2023/3/27 16:18
 **/
public class ResponseUtil {

    /**
     * 封装响应结果
     * @param response
     * @param commonResult
     */
    public static void responseJson(HttpServletResponse response,CommonResult commonResult){
        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            if(ObjectUtils.isNotEmpty(commonResult)){
                out.append(JSONObject.toJSONString(commonResult));
            }else{
                out.append(JSONObject.toJSONString(CommonResult.ok()));
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
